/**
Copyright (c) 2008 New Gonzo <john@newgonzo.com>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
**/


﻿package com.newgonzo.cannonball.browser.dom
{
	import flash.events.EventDispatcher;
	import flash.utils.Dictionary;
	import flash.utils.getQualifiedClassName;
	import flash.utils.getDefinitionByName;
	
	import com.newgonzo.commons.logging.Log;
	import com.newgonzo.commons.logging.ILogger;
	
	;
	
	import com.newgonzo.cannonball.w3c.dom.*;
	import com.newgonzo.cannonball.w3c.dom.views.IAbstractView;
	import com.newgonzo.cannonball.w3c.dom.views.IDocumentView;
	import com.newgonzo.cannonball.w3c.dom.traversal.IDocumentTraversal;
	import com.newgonzo.cannonball.w3c.dom.traversal.ITreeWalker;
	import com.newgonzo.cannonball.w3c.dom.traversal.NodeFilter;
	
	import com.newgonzo.cannonball.dom.*;
	import com.newgonzo.cannonball.w3c.dom.*;
	import com.newgonzo.cannonball.w3c.dom.traversal.ITreeWalker;
	
	import com.newgonzo.cannonball.browser.context.IContext;
	import com.newgonzo.cannonball.browser.actions.IAction;
	import com.newgonzo.cannonball.browser.rendering.*;
	
	import com.newgonzo.cannonball.browser.dom.DOMContext;
	
	public class DOMView extends EventDispatcher implements IView
	{
		protected static const log:ILogger = Log.getLogger(DOMView);
		
		private var namedRenderers:Dictionary;
		private var rendererAliases:Dictionary;
		private var domContext:IContext;
		
		public function DOMView(context:IContext)
		{
			domContext = context;
			namedRenderers = new Dictionary();
			rendererAliases = new Dictionary();
		}
		
		public function addRenderer(renderer:*, alias:String = ""):void
		{
			if(!renderer)
			{
				log.warn("Attempted to add null renderer with alias \"" + alias + "\"");
				return;
			}
			
			addNamedRenderer(renderer, alias);
		}
		
		public function removeRenderer(renderer:*):void
		{
			if(!renderer)
			{
				log.warn("Attempted to remove null renderer");
				return;
			}
			
			removeNamedRenderer(renderer);
		}
		
		public function getRenderer(name:String):IRenderer
		{
			log.debug("DOM.getRenderer(" + name + ")");
			
			try
			{
				var rendererSource:* = namedRenderers[name];
				var renderer:IRenderer;
				
				trace("rendererSource: " + rendererSource);
				
				if(rendererSource is Class)
				{
					renderer = new (rendererSource as Class)() as IRenderer;
				}
				else if(rendererSource is ITemplate)
				{
					renderer = (rendererSource as ITemplate).getRenderer(name);
					
					if(!renderer)
					{
						log.warn("Using template " + rendererSource + " as renderer \"" + name + "\"");
						renderer = rendererSource as IRenderer;
					}
				}
				else if(rendererSource is IRenderer)
				{
					renderer = rendererSource as IRenderer;
				}
				else
				{
					// try as class name
					var rendererClass:Class = getDefinitionByName(name) as Class;
					
					if(rendererClass)
					{
						renderer = new rendererClass() as IRenderer;
					}
				}
				
				if(!renderer)
				{
					log.error("No renderer found with name \"" + name + "\"");
					return null;
				}
				
				return renderer;
			}
			catch(e:Error)
			{
				log.error("No renderer found with name \"" + name + "\"");
			}
			
			return null;
		}
		
		public function clearRenderer(name:String):void
		{
			var renderer:* = namedRenderers[name];
			
			delete namedRenderers[name];
			
			if(renderer)
			{
				delete rendererAliases[renderer];
			}
		}
			
		
		protected function addTemplate(template:ITemplate):void
		{
			log.debug("DOMView.addTemplate(" + template + ")");
			
			var rendererNames:Array = template.rendererNames;
			var rendererName:String
			var renderer:IRenderer;
			
			for each(rendererName in rendererNames) 
			{
				log.debug("Adding renderer " + rendererName + " for " + template);
				namedRenderers[rendererName] = template;
			}
		}
		
		protected function removeTemplate(template:ITemplate):void
		{
			log.debug("DOMView.removeTemplate(" + template + ")");
			
			var rendererNames:Array = template.rendererNames;
			var rendererName:String
			var renderer:IRenderer;
			
			for each(rendererName in rendererNames) 
			{
				if(namedRenderers[rendererName] == template)
				{
					log.debug("Removing renderer " + rendererName + " for " + template);
					delete namedRenderers[rendererName];
				}
			}
		}
		
		protected function addNamedRenderer(renderer:*, alias:String = ""):void
		{
			log.info("Adding named renderer " + renderer + " with alias \"" + alias + "\"");
			
			if(alias.length)
			{
				namedRenderers[alias] = renderer;
				rendererAliases[renderer] = alias;
			}
			
			var qualName:String = getQualifiedClassName(renderer);
			var className:String = qualName.replace(/::/, ".");
			
			namedRenderers[qualName] = renderer;
			namedRenderers[className] = renderer;
			
			trace(namedRenderers[qualName]);
			trace(namedRenderers[className]);
			
			if(renderer is ITemplate)
			{
				addTemplate(renderer as ITemplate);
			}
		}
		
		protected function removeNamedRenderer(renderer:*):void
		{
			if(rendererAliases[renderer])
			{
				delete namedRenderers[rendererAliases[renderer]];
				delete rendererAliases[renderer];
			}
			
			var qualName:String = getQualifiedClassName(renderer);
			var className:String = qualName.replace(/::/, ".");
			
			delete namedRenderers[qualName];
			delete namedRenderers[className];
			
			if(renderer is ITemplate)
			{
				removeTemplate(renderer as ITemplate);
			}
		}
	}
}